//
//  ZCNavigationController.m
//  BaiSi
//
//  Created by 张灿 on 16/4/25.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCNavigationController.h"

@interface ZCNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation ZCNavigationController

+ (void)load
{
    //获取全局的UINavigationBar
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    //创建可变字典保存字体配置信息
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    
    //设置全局导航栏字体
    [navBar setTitleTextAttributes:attr];
    
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

//重写push方法,拦截跳转,在跳转之前判断是否为非根控制器,是的话就自定义返回按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count) {//子控制器个数大于0,说明不是根控制器
        //创建自定义按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
}

//返回上一个控制器
- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    //添加手势
    [self.view addGestureRecognizer:pan];
    //取消边缘侧滑返回
    self.interactivePopGestureRecognizer.enabled = NO;
    
    //实现全屏侧滑返回
    pan.delegate = self;

}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //大于1表示,如果不是根控制器就允许接收手势,实现侧滑返回
    return self.childViewControllers.count > 1;
}





@end
